/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Telas;

import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Guilherme
 */
public class TelaHorarios extends javax.swing.JInternalFrame {
    JFileChooser jfcfoto = new JFileChooser();
    String caminho;
    String nome;
    /**
     * Creates new form TelaHorários
     */
    //jLabel3.setIcon(new javax.swing.ImageIcon("Bibliotecas\\Documentos\\teste.jpg"));
    public TelaHorarios() {
        initComponents();
        fileChooser();
        if(TelaLogin.tipousuario.equals("coordenador")){
            jButtonEditarHorario.setVisible(true);
            jButtonSalvar.setVisible(true);
        }
        else if(TelaLogin.tipousuario.equals("aluno")){
            jButtonEditarHorario.setVisible(false);
            jButtonSalvar.setVisible(false);
        }
    }
    
    public void fileChooser(){
        jfcfoto.setFileFilter(new FileNameExtensionFilter("Image Files", "jpeg", "jpg", "png")); //seleciona apenas esses tipos de imagens
        jfcfoto.setAcceptAllFileFilterUsed(false); //nao aparece a opção de "Todos os Arquivos"
        jfcfoto.setFileSelectionMode(JFileChooser.FILES_ONLY); //não permite selecionar diretórios
        jfcfoto.setApproveButtonText("Selecionar"); //nome do botão coloca ai o nome do botão que vai selecionar, tipo "Selecionar", "Confirmar"...
        jfcfoto.setMultiSelectionEnabled(false);
        jfcfoto.setFileHidingEnabled(true);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBoxPeriodo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jEntrar = new javax.swing.JButton();
        jButtonEditarHorario = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabelPeriodoSelected = new javax.swing.JLabel();
        jButtonSalvar = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(657, 355));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Escolha seu período:");

        jComboBoxPeriodo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1º período", "2º período", "3º período", "4º período", "5º período", "6º período", "7º período", "8º período"}));
        jComboBoxPeriodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxPeriodoActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        jLabel2.setText("Horário Sugerido");

        jEntrar.setText("Entrar");
        jEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jEntrarActionPerformed(evt);
            }
        });

        jButtonEditarHorario.setText("Buscar imagem");
        jButtonEditarHorario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditarHorarioActionPerformed(evt);
            }
        });

        jLabel4.setText(" ");

        jLabelPeriodoSelected.setText("Nenhum período selecionado ainda");

        jButtonSalvar.setText("Salvar");
        jButtonSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jEntrar)
                            .addComponent(jComboBoxPeriodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jButtonSalvar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButtonEditarHorario, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 825, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(97, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabelPeriodoSelected)
                .addGap(446, 446, 446))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelPeriodoSelected)
                .addGap(18, 18, 18)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxPeriodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jEntrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 167, Short.MAX_VALUE)
                        .addComponent(jButtonEditarHorario)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSalvar)
                        .addGap(61, 61, 61))
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        setBounds(0, 0, 1154, 500);
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBoxPeriodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxPeriodoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBoxPeriodoActionPerformed

    private void jEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jEntrarActionPerformed
        if(jComboBoxPeriodo.getSelectedItem().toString().equals("1º período")){
        jLabel4.setIcon(new javax.swing.ImageIcon("src/Telas/periodo1.png"));
        nome = "periodo1";
        }
        else if(jComboBoxPeriodo.getSelectedItem().toString().equals("2º período")){
        jLabel4.setIcon(new javax.swing.ImageIcon("src/Telas/periodo2.png"));
        nome = "periodo2";
        }
        else if(jComboBoxPeriodo.getSelectedItem().toString().equals("3º período")){
        jLabel4.setIcon(new javax.swing.ImageIcon("src/Telas/periodo3.png"));
        nome = "periodo3";
        }
        else if(jComboBoxPeriodo.getSelectedItem().toString().equals("4º período")){
        jLabel4.setIcon(new javax.swing.ImageIcon("src/Telas/periodo4.png"));
        nome = "periodo4";
        }
        else if(jComboBoxPeriodo.getSelectedItem().toString().equals("5º período")){
        jLabel4.setIcon(new javax.swing.ImageIcon("src/Telas/periodo5.png"));
        nome = "periodo5";
        }
        else if(jComboBoxPeriodo.getSelectedItem().toString().equals("6º período")){
        jLabel4.setIcon(new javax.swing.ImageIcon("src/Telas/periodo6.png"));
        nome = "periodo6";
        }
        else if(jComboBoxPeriodo.getSelectedItem().toString().equals("7º período")){
        jLabel4.setIcon(new javax.swing.ImageIcon("src/Telas/periodo7.png"));
        nome = "periodo7";
        }
        else if(jComboBoxPeriodo.getSelectedItem().toString().equals("8º período")){
        jLabel4.setIcon(new javax.swing.ImageIcon("src/Telas/periodo8.png"));
        nome = "periodo8";
        }
        jLabelPeriodoSelected.setText("Periodo selecionado: "+jComboBoxPeriodo.getSelectedItem());
        // TODO add your handling code here:
    }//GEN-LAST:event_jEntrarActionPerformed

    private void jButtonEditarHorarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditarHorarioActionPerformed
           abrirFileChooser();
    }//GEN-LAST:event_jButtonEditarHorarioActionPerformed

    private void jButtonSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalvarActionPerformed
        imagem();
    }//GEN-LAST:event_jButtonSalvarActionPerformed
    
    
    private void abrirFileChooser(){
        int returnVal = jfcfoto.showOpenDialog(this);
        if(returnVal==JFileChooser.APPROVE_OPTION){
            caminho = jfcfoto.getSelectedFile().getAbsolutePath();
            ImageIcon icone = new ImageIcon(caminho);
            jLabel4.setIcon(new ImageIcon(icone.getImage().getScaledInstance(jLabel4.getWidth(), jLabel4.getHeight(), Image.SCALE_DEFAULT)));
        }
    }
    
    public void copiarImagem(File origem, File destino){
        try{
            FileChannel in = new FileInputStream(origem).getChannel();
            FileChannel out = new FileOutputStream(destino).getChannel();
            out.transferFrom(in, 0, in.size());
            
            in.close();
            out.close();
        } catch(IOException e){
            e.printStackTrace();
        }
    }
    
    private void imagem(){
        String nomeFoto = nome.trim();// erro de diretorio do destino ne?! per
        File original = new File(caminho);
        File copia = new File("C:\\Users\\Marcello\\Documents\\NetBeansProjects\\trunk\\trabalhoPOO\\ProgramaPoo\\src\\Telas\\"+nome+".png"); 
        
        if(!copia.exists()){
            copiarImagem(original, copia);
            caminho = copia.getAbsolutePath();
        } else{
            copia.delete();
            copiarImagem(original, copia);
            caminho = copia.getAbsolutePath();
      }
    }
    
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonEditarHorario;
    private javax.swing.JButton jButtonSalvar;
    private javax.swing.JComboBox jComboBoxPeriodo;
    private javax.swing.JButton jEntrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabelPeriodoSelected;
    // End of variables declaration//GEN-END:variables
}
